package com.tibbytang.opengllearn.empty

import android.opengl.GLES20
import android.opengl.GLES30
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import com.tibbytang.opengllearn.R
import com.tibbytang.opengllearn.util.Logger
import kotlinx.android.synthetic.main.activity_empty.*
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class EmptyActivity : AppCompatActivity() {

    private var mPosition:Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_empty)
        empty_surface_view.setEGLContextClientVersion(2)
        empty_surface_view.setEGLConfigChooser(8 , 8, 8, 8, 16, 4);
        val render = EmptyRender()
        empty_surface_view.setRenderer(render)
        empty_surface_view.renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY

        val list = mutableListOf<String>(
            "红色",
            "绿色",
            "蓝色"
        )

        empty_spinner.adapter = object : ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            android.R.id.text1,
            list
        ) {
        }
        empty_spinner.onItemSelectedListener =object :AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                mPosition = p2
                empty_surface_view.requestRender()
            }

        }
    }

    inner class EmptyRender : GLSurfaceView.Renderer {
        override fun onDrawFrame(p0: GL10?) {
            Logger.info("onDrawFrame")
            when(mPosition){
                0 -> GLES20.glClearColor(1.0f,0.0f,0.0f,1.0f)
                1 -> GLES20.glClearColor(0.0f,1.0f,0.0f,1.0f)
                2 -> GLES20.glClearColor(0.0f,0.0f,1.0f,1.0f)
            }
            GLES20.glClear(GLES30.GL_COLOR_BUFFER_BIT)
        }

        override fun onSurfaceChanged(p0: GL10?, p1: Int, p2: Int) {
            Logger.info("onSurfaceChanged")
            GLES20.glViewport(0, 0, p1, p2)
        }

        override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
            Logger.info("onSurfaceCreated")
            GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f)
        }

    }

}
